package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingLevelDto;
import com.zzyl.entity.NursingLevel;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.NursingLevelExistNameException;
import com.zzyl.mapper.NursingLevelMapper;
import com.zzyl.service.NursingLevelService;
import com.zzyl.vo.NursingLevelVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class NursingLevelServiceImpl implements NursingLevelService {

    @Autowired
    private NursingLevelMapper nursingLevelMapper;

    /**
     * 查询所有护理等级
     * @return
     */
    @Override
    public List<NursingLevelVo> getListAll() {
        // 调用Mapper层查询所有护理等级
        List<NursingLevelVo> nursingLevelList = nursingLevelMapper.getListAll();
        // 响应结果
        return nursingLevelList;
    }

    /**
     * 新增护理等级
     * @param nursingLevelDto
     */
    @Override
    public void addNursingLevel(NursingLevelDto nursingLevelDto) {
        NursingLevel nursingLevel = new NursingLevel();

        // 拷贝属性
        BeanUtils.copyProperties(nursingLevelDto, nursingLevel);
        Integer planId = nursingLevelDto.getPlanId();
        nursingLevel.setPlanId(planId.toString());

        // 调用Mapper层新增护理等级
        try {
            nursingLevelMapper.addNursingLevel(nursingLevel);
        } catch (Exception e) {
            throw new NursingLevelExistNameException(BasicEnum.NURSING_LEVEL_EXIST_NAME);
        }
    }

    /**
     * 条件分页查询护理等级
     * @param pageNum
     * @param pageSize
     * @param name
     * @param status
     * @return
     */
    @Override
    public PageResponse<NursingLevelVo> getByPage(Integer pageNum, Integer pageSize, String name, Integer status) {
        PageHelper.startPage(pageNum, pageSize);

        // 调用Service层条件分页查询护理等级
        Page<NursingLevelVo> page = nursingLevelMapper.getByPage(name, status);
        // 创建PageResponse<NursingLevelVo>对象,添加字段
        PageResponse<NursingLevelVo> pageResponse = new PageResponse<>(page);
        pageResponse.setRecords(page.getResult());

        // 响应数据
        return pageResponse;
    }

    /**
     * 根据id查询护理等级
     * @param id
     * @return
     */
    @Override
    public NursingLevelVo getById(Integer id) {
        // 调用Mapper层根据id查询护理等级
        NursingLevelVo nursingLevelVo = nursingLevelMapper.getById(id);
        // 响应数据
        return nursingLevelVo;
    }

    /**
     * 修改护理等级
     * @param nursingLevelDto
     */
    @Override
    public void updateNursingLevel(NursingLevelDto nursingLevelDto) {
        NursingLevel nursingLevel = new NursingLevel();
        // 属性拷贝
        BeanUtils.copyProperties(nursingLevelDto, nursingLevel);
        // 调用Mapper层根据id修改护理等级
        try {
            nursingLevelMapper.updateNursingLevel(nursingLevel);
        } catch (Exception e) {
            throw new NursingLevelExistNameException(BasicEnum.NURSING_LEVEL_EXIST_NAME);
        }
    }

    /**
     * 根据id删除护理等级
     * @param id
     */
    @Override
    public void deleteById(Integer id) {
        // 调用Mapper层根据id删除护理等级
        nursingLevelMapper.deleteById(id);
    }

    /**
     * 启用或禁用护理等级
     * @param id
     * @param status
     */
    @Override
    public void startOrStop(Integer id, Integer status) {
        NursingLevel nursingLevel = new NursingLevel();
        nursingLevel.setId(Long.valueOf(id));
        nursingLevel.setStatus(status);
        // 调用Mapper层根据id修改护理等级状态
        nursingLevelMapper.updateNursingLevel(nursingLevel);
    }
}
